clear 

cd "$root/Results/tables_graphs/appendix"

use "$root/Data/Original/NEUZU_TOT.dta", clear

*Only create quarterly averages: 

gen td=dofm(month_year)
gen year=yofd(td)
drop td month_year


**Add the population data: 

gcollapse (sum) nb_cars_tot nb_cars_E (firstnm) year, by(reg_canton quart_year)

merge m:1 year reg_canton using "$root/Data/Original/population.dta", keepusing(pop)

gen share=100*nb_cars_tot/pop
gen EV_share=nb_cars_E/nb_cars_tot

frame put _all, into(Wallis)

frame change Wallis

drop if reg_canton=="SH" | reg_canton=="AI" | reg_canton=="TI" | reg_canton=="TG" | reg_canton=="VD"

drop if missing(share)

gen treat=(reg_canton=="VS")

gcollapse (mean) share EV_share, by(treat quart_year)

local treat_time=quarterly("2020Q4","YQ")

di `treat_time'


twoway (line share quart_year if treat==1, sort lpattern(solid) lwidth(medthick) lcolor(blue) ) (line share quart_year if treat==0, sort lpattern(dash) lwidth(medthick) lcolor(red)), ytitle(Population adjusted new vehicle registrations) xtitle(Time) legend(order(1 "Valais" 2 "Average of non-treated cantons")) clegend(span) graphregion(fcolor(white) lcolor(none)) plotregion(fcolor(white)) xline(`treat_time', lpattern(dot) lwidth(medthick)  lcolor(black))
graph export "VS_quart_avg_reg.png", replace


twoway (line EV_share quart_year if treat==1, sort lpattern(solid) lwidth(medthick) lcolor(blue) ) (line EV_share quart_year if treat==0, sort lpattern(dash) lwidth(medthick) lcolor(red)), ytitle(Share of EVs in total registrations) xtitle(Time) legend(order(1 "Valais" 2 "Average of non-treated cantons")) clegend(span) graphregion(fcolor(white) lcolor(none)) plotregion(fcolor(white)) xline(`treat_time', lpattern(dot) lwidth(medthick)  lcolor(black))
graph export "VS_quart_avg_EVreg.png", replace


**Also check for Thurgau were the subsidy was introduced slightly earlier: 

frame change default

frame drop Wallis

frame put _all, into(Thurgau)

frame change Thurgau

drop if reg_canton=="SH" | reg_canton=="AI" | reg_canton=="TI" | reg_canton=="VS" | reg_canton=="VD"

drop if missing(share)

gen treat=(reg_canton=="TG")


local treat_time=quarterly("2019Q1","YQ")

di `treat_time'


gcollapse (mean) share EV_share, by(treat quart_year)

twoway (line share quart_year if treat==1, sort lpattern(solid) lwidth(medthick) lcolor(blue) ) (line share quart_year if treat==0, sort lpattern(dash) lwidth(medthick) lcolor(red)), ytitle(Population adjusted new vehicle registrations) xtitle(Time) legend(order(1 "Thurgau" 2 "Average of non-treated cantons")) clegend(span) graphregion(fcolor(white) lcolor(none)) plotregion(fcolor(white)) xline(`treat_time', lpattern(dot) lwidth(medthick)  lcolor(black))

graph export "TG_quart_avg_reg.png", replace


twoway (line EV_share quart_year if treat==1, sort lpattern(solid) lwidth(medthick) lcolor(blue) ) (line EV_share quart_year if treat==0, sort lpattern(dash) lwidth(medthick) lcolor(red)), ytitle(Share of EVs in total registrations) xtitle(Time) legend(order(1 "Thurgau" 2 "Average of non-treated cantons")) clegend(span) graphregion(fcolor(white) lcolor(none)) plotregion(fcolor(white)) xline(`treat_time', lpattern(dot) lwidth(medthick)  lcolor(black))
 graph export "TG_quart_avg_EVreg.png", replace

